package cn.alone.pay.service.impl;

import cn.alone.basic.BusinessException;
import cn.alone.basic.service.impl.BaseServiceImpl;
import cn.alone.order.service.IProductOrderService;
import cn.alone.pay.domain.AlipayInfo;
import cn.alone.pay.domain.PayBill;
import cn.alone.pay.mapper.AlipayInfoMapper;
import cn.alone.pay.mapper.PayBillMapper;
import cn.alone.pay.service.IPayBillService;
import cn.alone.pay.util.AliPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 0 支付宝 1 微信  2 银联
     * @param billTmp
     * @return
     */
    @Override
    public String pay(PayBill billTmp) {
        if(billTmp == null){
            throw new BusinessException("异常1");
        }
        System.out.println(".............>>>>"+billTmp);
        PayBill bill = payBillMapper.loadByUnionPaySn(billTmp.getUnionPaySn());
        System.out.println("==================================="+bill);
        if(bill == null){
            throw new BusinessException("异常2");
        }
        Long payChannel = bill.getPayChannel();
        switch (payChannel.intValue()) {
            case 0:{
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AliPayUtils.pay(bill, info);
            }
            case 1:{

            }
            case 2:{

            }
            default: break;
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
